package com.centling.smartSealForPhone.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.bean.ReviewListBean;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.ImageUtil;
import com.centling.smartSealForPhone.view.CircleImageView;

import java.util.List;

/**
 * @author fionera
 */
public class ReviewListAdapter
        extends RecyclerView.Adapter {
    private Context context;
    private List<ReviewListBean.RowsBean> list;

    private OnItemClickListener mOnItemClickListener;
    private OnButtonClickListener mOnButtonClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnButtonClickListener(OnButtonClickListener mOnButtonClickListener) {
        this.mOnButtonClickListener = mOnButtonClickListener;
    }

    public interface OnItemClickListener {
        void onClick(View v, int pos);
    }

    public interface OnButtonClickListener {
        void onClick(View v, int pos);
    }

    public ReviewListAdapter(Context context, List<ReviewListBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReviewViewHolder(
                LayoutInflater.from(context).inflate(R.layout.rv_review_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ReviewViewHolder viewHolder = (ReviewViewHolder) holder;
        ImageUtil.loadAvatarImage(list.get(position).getApplicantUserAvatar(),
                viewHolder.avatar, R.drawable.iv_avatar_default);
        viewHolder.user.setText(list.get(position).getApplicantUserName());
        if (TextUtils.isEmpty(list.get(position).getReviewFailReason())) {
            viewHolder.reason.setText("无复审失败理由");
        } else {
            viewHolder.reason.setText(list.get(position).getReviewFailReason());
        }
        viewHolder.time.setText(list.get(position).getApplyDate());

        if (TextUtils.equals(list.get(position).getReviewResultType(), "ILLEGAL")) {
            viewHolder.tvReplenish.setText("非法用印");
            viewHolder.tvReplenish.setClickable(false);
            viewHolder.tvReplenish.setTextColor(ContextCompat.getColor(context, R.color.red));
            viewHolder.tvReplenish.setBackground(null);
        } else if (TextUtils.equals(list.get(position).getReviewResultType(), "RESHOOT")) {
            if (TextUtils.equals(list.get(position).getIsValid(), "0")) {
                viewHolder.tvReplenish.setText("补拍");
                viewHolder.tvReplenish.setClickable(true);
                viewHolder.tvReplenish.setTextColor(ContextCompat.getColor(context, R.color.blue1));
                viewHolder.tvReplenish.setBackgroundResource(R.drawable.sl_bg_white_blue1_white_blue2_corner4);
                viewHolder.tvReplenish.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnButtonClickListener != null) {
                            mOnButtonClickListener.onClick(v, position);
                        }
                    }
                });
            } else {
                viewHolder.tvReplenish.setText("复审失败");
                viewHolder.tvReplenish.setClickable(false);
                viewHolder.tvReplenish.setTextColor(ContextCompat.getColor(context, R.color.black));
                viewHolder.tvReplenish.setBackground(null);
            }
        }else{
            viewHolder.tvReplenish.setText("未知状态");
            viewHolder.tvReplenish.setClickable(false);
            viewHolder.tvReplenish.setTextColor(ContextCompat.getColor(context, R.color.black));
            viewHolder.tvReplenish.setBackground(null);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private class ReviewViewHolder
            extends RecyclerView.ViewHolder {
        private CircleImageView avatar;
        private TextView user;
        private TextView time;
        private TextView reason;
        private TextView tvReplenish;

        ReviewViewHolder(View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.civ_review_list_preview);
            user = itemView.findViewById(R.id.tv_review_list_name);
            time = itemView.findViewById(R.id.tv_review_list_time);
            reason = itemView.findViewById(R.id.tv_review_list_reason);
            tvReplenish = itemView.findViewById(R.id.tv_review_list_republish);
        }
    }
}
